package com.ruoyi.baoming.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.baoming.domain.ExamCenterCity;
import com.ruoyi.baoming.service.IExamCenterCityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 考点盟市信息Controller
 * 
 * @author xiaosong
 * @date 2024-11-20
 */
@Api(tags = "考试管理-中心城市")
@RestController
@RequestMapping("/baoming/city")
public class ExamCenterCityController extends BaseController
{
    @Autowired
    private IExamCenterCityService examCenterCityService;

    /**
     * 查询考点盟市信息列表
     */
    @PreAuthorize("@ss.hasPermi('baoming:city:list')")
    @GetMapping("/list")
    @ApiOperation("考试中心城市管理 - 分页查询列表")
    public TableDataInfo list(ExamCenterCity examCenterCity)
    {
        startPage();
        List<ExamCenterCity> list = examCenterCityService.selectExamCenterCityList(examCenterCity);
        return getDataTable(list);
    }

    /**
     * 导出考点盟市信息列表
     */
    @PreAuthorize("@ss.hasPermi('baoming:city:export')")
    @Log(title = "考点盟市信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("考试中心城市管理 - 导出数据")
    public void export(HttpServletResponse response, ExamCenterCity examCenterCity)
    {
        List<ExamCenterCity> list = examCenterCityService.selectExamCenterCityList(examCenterCity);
        ExcelUtil<ExamCenterCity> util = new ExcelUtil<ExamCenterCity>(ExamCenterCity.class);
        util.exportExcel(response, list, "考点盟市信息数据");
    }

    /**
     * 获取考点盟市信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('baoming:city:query')")
    @GetMapping(value = "/{id}")
        @ApiOperation("考试中心城市管理 - 根据ID获取详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(examCenterCityService.selectExamCenterCityById(id));
    }

    /**
     * 新增考点盟市信息
     */
    @PreAuthorize("@ss.hasPermi('baoming:city:add')")
    @Log(title = "考点盟市信息", businessType = BusinessType.INSERT)
    @PostMapping
        @ApiOperation("考试中心城市管理 - 新增数据")
    public AjaxResult add(@RequestBody ExamCenterCity examCenterCity)
    {
        return toAjax(examCenterCityService.insertExamCenterCity(examCenterCity));
    }

    /**
     * 修改考点盟市信息
     */
    @PreAuthorize("@ss.hasPermi('baoming:city:edit')")
    @Log(title = "考点盟市信息", businessType = BusinessType.UPDATE)
    @PutMapping
        @ApiOperation("考试中心城市管理 - 修改数据")
    public AjaxResult edit(@RequestBody ExamCenterCity examCenterCity)
    {
        return toAjax(examCenterCityService.updateExamCenterCity(examCenterCity));
    }

    /**
     * 删除考点盟市信息
     */
    @PreAuthorize("@ss.hasPermi('baoming:city:remove')")
    @Log(title = "考点盟市信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
        @ApiOperation("考试中心城市管理 - 删除数据")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(examCenterCityService.deleteExamCenterCityByIds(ids));
    }

    /**
     * 上传excel文件，实现批量导入数据
     */
//    @PreAuthorize("@ss.hasPermi('baoming:city:importData')")
    @Log(title = "考点盟市信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
        @ApiOperation("考试中心城市管理 - importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws Exception{
        // 1. 基础文件检查
        if (file.isEmpty()) {
            return AjaxResult.error("文件不能为空");
        }
        if (!file.getOriginalFilename().matches(".*\\.(xls|xlsx)$")) {
            return AjaxResult.error("仅支持Excel文件");
        }

        ExcelUtil<ExamCenterCity> util = new ExcelUtil<>(ExamCenterCity.class);
        System.out.println(file.getInputStream());
        List<ExamCenterCity> deviceList = util.importExcel(file.getInputStream());
        System.out.println(deviceList);

        for (ExamCenterCity d : deviceList) {
            System.out.println( d);

            examCenterCityService.insertExamCenterCity(d);
        }
        return AjaxResult.success("导入成功" + deviceList.size() + "条数据");
    }

}
